<?php
class PasswordsController extends Zend_Controller_Action {
	public function preDispatch() { 
		$auth = Zend_Auth::getInstance(); 
		if (!$auth->hasIdentity()) { 
			$this->_redirect('login'); 
		}
		else {
			if(!NerdPass_Acl::isAllowed($auth->getIdentity(), 'passwords')) {
				$this->_forward('denied', 'error');
			}
		}
	}
	
	
	public function addAction() {
		if (count($_POST) <= 0) {
			$this->_redirect('/');
		}
		
		/* Validate form */
		$validators = array(
			'category' => array('presence' => 'required'),
			'summary' => array('presence' => 'required'),
			'username' => array('presence' => 'required'),
			'password' => array('presence' => 'required'),
		);
		$input = new Zend_Filter_Input(NULL, $validators, $_POST);
		if ($input->hasInvalid() || $input->hasMissing()) {
			$this->view->message = $input->getMessages();
		}
		else {
			$this->view->message = "Password added successfully!";
			// Add password to database
			NerdPass_Data::addPassword($_POST['category'], $_POST['summary'], $_POST['username'], $_POST['password'], $_POST['website'], $_POST['notes']);
		}
		$messagesNamespace = new Zend_Session_Namespace('Messages');
		$messagesNamespace->passwordMessage = $this->view->message;

		
		$this->view->categories = NerdPass_Data::getCategories();
		$this->view->passwords = NerdPass_Data::getPasswords();
		
		// Get uid
		$request = $this->getRequest();
		$uid = $request->getParam('uid');
		$this->view->uid = $uid;
		
		if (!isset($_SERVER['HTTP_REFERER'])) {
			$refurl = '/';
		}
		else {
			$refurl = $_SERVER['HTTP_REFERER'];
		}
		//$this->_redirect($refurl);
		$this->_redirect($refurl);
	}
	
	public function viewAction() {
		$this->view->categories = NerdPass_Data::getCategories();
		$this->view->passwords = NerdPass_Data::getPasswords();
		
		// Get uid
		$request = $this->getRequest();
		$uid = $request->getParam('uid');
		$this->view->uid = $uid;
		
		$pswd = NerdPass_Data::getPassword($uid);
		
		$this->view->username = $pswd['username'];
		$this->view->password = $pswd['password'];
		$this->view->summary = $pswd['summary'];
		$this->view->notes = $pswd['notes'];
		$this->view->website = $pswd['website'];
	}
	
	public function editAction() {
		$this->view->categories = NerdPass_Data::getCategories();
		$this->view->passwords = NerdPass_Data::getPasswords();
		
		// Get uid
		$request = $this->getRequest();
		$uid = $request->getParam('uid');
		$this->view->uid = $uid;
		
		$pswd = NerdPass_Data::getPassword($uid);
		
		$this->view->username = $pswd['username'];
		$this->view->password = $pswd['password'];
		$this->view->summary = $pswd['summary'];
		$this->view->notes = $pswd['notes'];
		$this->view->website = $pswd['website'];
		$this->view->category = $pswd['category'];
	}
	
	public function saveAction() {
		// Validate fields
		if (count($_POST) <= 0) {
			$this->_redirect('/');
		}
		
		// Get uid
		$request = $this->getRequest();
		$uid = $request->getParam('uid');
		$this->view->uid = $uid;
		
		/* Validate form */
		$validators = array(
			'category' => array('presence' => 'required'),
			'summary' => array('presence' => 'required'),
			'username' => array('presence' => 'required'),
			'password' => array('presence' => 'required'),
		);
		$input = new Zend_Filter_Input(NULL, $validators, $_POST);
		if ($input->hasInvalid() || $input->hasMissing()) {
			$this->view->message = $input->getMessages();
		}
		else {
			$this->view->message = "Password added successfully!";
			// Save password changes to database
			NerdPass_Data::editPassword($uid, $_POST['category'], $_POST['summary'], $_POST['username'], $_POST['password'], $_POST['website'], $_POST['notes']);
			$this->_redirect($this->view->url(array('uid' => $uid), 'viewpass'));
		}
		
		$this->view->categories = NerdPass_Data::getCategories();
		$this->view->passwords = NerdPass_Data::getPasswords();
		
		$pswd = NerdPass_Data::getPassword($uid);
		$this->view->username = $pswd['username'];
		$this->view->password = $pswd['password'];
		$this->view->summary = $pswd['summary'];
		$this->view->notes = $pswd['notes'];
		$this->view->website = $pswd['website'];
		$this->view->category = $pswd['category'];
		
		$this->render('edit');
	}
	
	public function deleteAction() {		
		// Get uid
		$request = $this->getRequest();
		$uid = $request->getParam('uid');
		$this->view->uid = $uid;
		
		$pswd = NerdPass_Data::getPassword($uid);
		$this->view->summary = $pswd['summary'];
		
		// Delete item
		NerdPass_Data::deletePassword($uid);
		
		$this->view->categories = NerdPass_Data::getCategories();
		$this->view->passwords = NerdPass_Data::getPasswords();
		
		$this->view->returnUrl = '/';
		
		$this->view->message = "Password with summary \"" . $pswd['summary'] . "\" has been deleted.";
		$messagesNamespace = new Zend_Session_Namespace('Messages');
		$messagesNamespace->passwordMessage = $this->view->message;

		
		$this->_redirect($refurl);
	}
}
